/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.util.WebClientL10nUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetAvailableLocales
/*     */   extends AccountDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  41 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  42 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  44 */     if (!canAccessAccount(zsc, account)) {
/*  45 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  48 */     Locale displayLocale = getDisplayLocale(account, context);
/*     */     
/*     */ 
/*  51 */     Locale[] installedLocales = WebClientL10nUtil.getLocales(displayLocale);
/*     */     
/*     */ 
/*  54 */     Set<String> allowedLocales = account.getMultiAttrSet("zimbraAvailableLocale");
/*     */     
/*  56 */     Locale[] availLocales = null;
/*  57 */     if (allowedLocales.size() > 0) {
/*  58 */       availLocales = computeAvailLocales(installedLocales, allowedLocales);
/*     */     } else {
/*  60 */       availLocales = installedLocales;
/*     */     }
/*     */     
/*  63 */     Element response = zsc.createElement(AccountConstants.GET_AVAILABLE_LOCALES_RESPONSE);
/*  64 */     for (Locale locale : availLocales) {
/*  65 */       if (locale == null) break;
/*  66 */       ToXML.encodeLocale(response, locale, displayLocale);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  71 */     return response;
/*     */   }
/*     */   
/*     */   private Locale getDisplayLocale(Account acct, Map<String, Object> context) throws ServiceException
/*     */   {
/*  76 */     String locale = acct.getAttr("zimbraPrefLocale", false);
/*     */     
/*     */ 
/*  79 */     if (StringUtil.isNullOrEmpty(locale)) {
/*  80 */       HttpServletRequest req = (HttpServletRequest)context.get("servlet.request");
/*  81 */       if (req != null) {
/*  82 */         locale = req.getHeader("Accept-Language");
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*  88 */     if (StringUtil.isNullOrEmpty(locale)) {
/*  89 */       return Provisioning.getInstance().getLocale(acct);
/*     */     }
/*  91 */     return L10nUtil.lookupLocale(locale);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Locale[] computeAvailLocales(Locale[] installedLocales, Set<String> allowedLocales)
/*     */   {
/* 105 */     Locale[] availLocales = new Locale[installedLocales.length];
/* 106 */     int i = 0;
/* 107 */     for (Locale locale : installedLocales)
/*     */     {
/*     */ 
/* 110 */       String localeId = locale.toString();
/* 111 */       String language = locale.getLanguage();
/* 112 */       String country = locale.getCountry();
/* 113 */       String variant = locale.getVariant();
/*     */       
/* 115 */       if (!StringUtil.isNullOrEmpty(variant)) {
/* 116 */         if ((allowedLocales.contains(language)) || (allowedLocales.contains(language + "_" + country)) || (allowedLocales.contains(localeId))) {
/* 117 */           availLocales[(i++)] = locale;
/*     */         }
/* 119 */       } else if (!StringUtil.isNullOrEmpty(country)) {
/* 120 */         if ((allowedLocales.contains(language)) || (allowedLocales.contains(localeId))) {
/* 121 */           availLocales[(i++)] = locale;
/*     */         }
/*     */       }
/* 124 */       else if (allowedLocales.contains(localeId)) {
/* 125 */         availLocales[(i++)] = locale;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 130 */     return availLocales;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetAvailableLocales.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */